package com.wutuo.institution.provider.component;

import cn.apestar.base.SpringContextHolder;
import cn.apestar.friend.provider.service.WtEliteService;
import cn.apestar.user.api.SysAreaAPIService;
import cn.apestar.user.provider.institution.response.InstitutionResponse;
import cn.apestar.user.provider.institution.service.SysAreaService;
import cn.apestar.util.CommonUtil;
import com.wutuo.common.UserAndInstitutionWTService;
import com.wutuo.institution.api.response.InstitutionAllResponse;
import com.wutuo.institution.provider.domain.UserInstitution;
import com.wutuo.school.api.SchoolAPIService;
import com.wutuo.school.provider.service.SchoolService;
import com.wutuo.wtservice.api.ServiceAPIService;
import com.wutuo.wtservice.provider.service.ServiceService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class InstitutionDataConvert {

    private static SchoolAPIService schoolService =
            SpringContextHolder.getBean(SchoolService.class);

    private static SysAreaAPIService sysAreaService =
            SpringContextHolder.getBean(SysAreaService.class);

    private static ServiceAPIService serviceService =
            SpringContextHolder.getBean(ServiceService.class);

    private static UserAndInstitutionWTService userAndInstituionWTService =
            SpringContextHolder.getBean("userAndInstitutionWTService");

    private static cn.apestar.user.provider.institution.service.UserAndInstitutionService userAndInstitutionService =
            SpringContextHolder.getBean(cn.apestar.user.provider.institution.service.UserAndInstitutionService.class);

    private static WtEliteService wtEliteService =
            SpringContextHolder.getBean(WtEliteService.class);

    // 翻译
    public static <T> void convertToInstitutionResponseList(List<T> list, boolean flag) {

        if (flag) {
            schoolService.getSchoolByIds(list, "schoolId", "school");
            sysAreaService.getAreaServiceById(list, "province", "shengName");
            sysAreaService.getAreaServiceById(list, "city", "cityName");
            sysAreaService.getAreaServiceById(list, "county", "xianName");
        }
    }

    // 翻译
    public static <T> void convertTolistPageInstitutionResponseList(List<InstitutionResponse> list,
                                                                    boolean flag) {

        schoolService.getSchoolByIds(list, "schoolId", "school");
        serviceService.getUseServiceByInstationIds(list, "id", "tgTypeList");
        userAndInstitutionService.getUserListByUserId(list);
    }

    /**
     * 翻译机构明细信息
     *
     * @param s
     * @return void
     * @author lvh 2016年6月23日 下午4:43:11</br>
     */
    public static InstitutionAllResponse convertToInstitutionResponse(InstitutionResponse s, Long userId) {

        InstitutionAllResponse institutionAllResponse = new InstitutionAllResponse();
        List<InstitutionResponse> list = new ArrayList();
        list.add(s);
        // 午托服务
        serviceService.getUseServiceByInstationIds(list, "id", "tgTypeList");
        CommonUtil.copyProperties(institutionAllResponse, s);
        // 最新动态
        institutionAllResponse
                .setFriend(wtEliteService.getWtEliteResponseByInstitutionId(s.getId(), userId));
        // 机构人员
        Map mapUser = userAndInstituionWTService.getUserListByUserId(s.getId());
        List<UserInstitution> listMaster = (List<UserInstitution>) mapUser.get("listMaster");
        List<UserInstitution> listTeacher = (List<UserInstitution>) mapUser.get("listTeacher");
        institutionAllResponse.setTeacher(listTeacher);
        if (listMaster.size() > 0)
            institutionAllResponse.setMaster(listMaster.get(0));
        return institutionAllResponse;
    }

}
